package com.bootdo.acitvity.DDD.domain.repository.persistence;

import com.bootdo.acitvity.DDD.domain.CenterBatch;
import com.bootdo.acitvity.DDD.domain.factory.StockFactory;
import com.bootdo.acitvity.DDD.domain.repository.facade.StockRepository;
import com.bootdo.acitvity.DDD.domain.repository.mapper.SkuBatchChannelMapper;
import com.bootdo.acitvity.DDD.domain.repository.mapper.SkuBatchMapper;
import com.bootdo.acitvity.DDD.domain.vo.ChannelSkuBatch;
import com.bootdo.acitvity.DDD.domain.vo.SkuBatch;
import com.bootdo.acitvity.DDD.infrastrcture.redis.RedisBatch;
import com.bootdo.acitvity.DDD.infrastrcture.redis.RedisBatchChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class StockDBRepository implements StockRepository {

    @Resource
    private StockFactory stockFactory;

    @Resource
    SkuBatchMapper skuBatchMapper;

    @Resource
    private SkuBatchChannelMapper skuBatchChannelMapper;

    @Override
    public CenterBatch getById(String batchId) {
        // 销售总批次
        RedisBatch skuBatch = stockFactory.getSkuBatch(batchId);

        // 渠道批次
        List<RedisBatchChannel> skuChannelBatchList = stockFactory.getSkuChannelBatch(batchId);

        skuBatch.setList(skuChannelBatchList);

        // 封装聚合根
        CenterBatch centerBatch = stockFactory.buildCenterBatch(skuBatch);

        return centerBatch;

    }

    @Override
    public void save(CenterBatch centerBatch) {
        SkuBatch skuBatch = stockFactory.buildSkuBatch(centerBatch);
        skuBatchMapper.saveSkuBatch(skuBatch);

        List<ChannelSkuBatch> channelSkuBatches = stockFactory.buildSkuBatchChannel(centerBatch);
        skuBatchChannelMapper.saveSkuChannelBatch(channelSkuBatches);

    }
}
